package com.qys.livesMall.app.user.utils;

import org.apache.http.HttpHost;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ProxyPostRequest {

    public static String post(String url, String param) {
        String proxyHost = "183.213.200.144";
        int proxyPort = 29205;

        try {
            // Set proxy information
            HttpHost proxy = new HttpHost(proxyHost, proxyPort);

            // Create HttpClient
            CloseableHttpClient httpClient = HttpClients.custom().setProxy(proxy).build();

            // Create POST request
            HttpPost httpPost = new HttpPost(url);

            // Add POST data if needed
            String postData = param; // Replace with your POST data
            StringEntity entity = new StringEntity(postData);
            httpPost.setEntity(entity);
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");

            // Execute the request
            CloseableHttpResponse response = httpClient.execute(httpPost);

            // Read response
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line;
            StringBuilder result = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }
            reader.close();

            // Print response
            System.out.println("Response: " + result.toString());

            // Close resources
            response.close();
            httpClient.close();

            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
